<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="js/mui.min.js"></script>
		<script src="js/jquery-2.1.4.min.js"></script>
		<link href="css/mui.min.css" rel="stylesheet" />
		<style>
			.mui-table-view-cell:after {
				position: absolute;
				right: 0px;
				bottom: 0;
				left: 0px;
				height: 1px;
				content: '';
				-webkit-transform: scaleY(.5);
				transform: scaleY(.5);
				background-color: #c8c7cc;
			}
		</style>
	</head>
	<body>
		<div id="pullrefresh" class="mui-content mui-scroll-wrapper">
			<div class="mui-scroll">
				<ul class="mui-table-view" id="container">
					<div id="itemTemplate" style="display: none;">
						<li class="mui-table-view-cell" style="font-size: 12px;">
							<div style="font-size: 12px;color: blue;">
								<a class="article-title"></a>
							</div>
							<div class="article-summary" href="">
							</div>
						</li>
					</div>
				</ul>
			</div>
		</div>
		<div id="selectUrl" style="display: none;">
			<input id="selectUrlInput" type="text" />
		</div>
		<script type="text/javascript" charset="utf-8">
			mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						callback: pullupRefresh
					}
				},
				gestureConfig: {
					tap: true, //默认为true
					doubletap: true, //默认为false
					longtap: true, //默认为false
					swipe: true, //默认为true
					drag: true, //默认为true
					hold: false, //默认为false，不监听
					release: false //默认为false，不监听
				}
			});

			mui(".mui-table-view").on('longtap', '.article-summary', function() {
				var url = this.getAttribute("href");
				var flag = confirm("用本地浏览器打开当前文章？");
				if(flag){
					plus.runtime.openURL(url);
				}
			});

			function viewArticle(href) {
				var waitingWindow = plus.nativeUI.toast("页面正在加载，需要稍等一会儿");
				var obj = plus.webview.open(href, "articleView", {
					top: '45px',
					bottom: '0px',
					scalable: true
				});
			}
			var firstId = 0;
			var lastId = 0;
			var loadFlag = true;
			var url = 'http://shumanu.com/';
			var version = "1.0.3";

			function pulldownRefresh() {
				var temp = "";
				if (firstId != 0) {
					temp = "Default.aspx?Action=PullDown&Id=" + firstId
				}
				mui.get(url + temp, {}, function(data) {
					if (data.length < 1) {
						mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
						return;
					}
					var container = $("#container");
					var li = $("#itemTemplate");
					firstId = data[0].news_id;
					if (lastId == 0) {
						lastId = data[data.length - 1].news_id;
					}
					for (var i = 0; i < data.length; i++) {
						li.find(".article-title").text(data[i].news_title).attr("href", "javascript:viewArticle('" + data[i].news_url + "');");
						li.find(".article-summary").text(data[i].news_summary).attr("href", data[i].news_url);
						container.prepend(li.html());
					}
				}, 'json');
				mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
			}

			function pullupRefresh() {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
				var temp = "";
				if (lastId != 0) {
					temp = "Default.aspx?Action=PullUp&Id=" + lastId
				}
				mui.get(url + temp, {}, function(data) {
					if (data.length < 1) {
						mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
						return;
					}
					if (loadFlag) {
						plus.navigator.closeSplashscreen();
						mui.get(url + 'Default.aspx?Action=CheckVersion', {}, function(data) {
							if (data != version) {
								var flag = confirm("有新版本，是否更新");
								if (flag) {
									plus.runtime.openURL(url + "DeveloperNews.apk");
								}
							}
						});
						loadFlag = false;
					}
					var container = $("#container");
					var li = $("#itemTemplate");
					lastId = data[data.length - 1].news_id;
					if (firstId == 0) {
						firstId = data[0].news_id;
					}
					for (var i = 0; i < data.length; i++) {
						li.find(".article-title").text(data[i].news_title).attr("href", "javascript:viewArticle('" + data[i].news_url + "');");
						li.find(".article-summary").text(data[i].news_summary).attr("href", data[i].news_url);
						container.append(li.html());
					}
				}, 'json');
			}
			if (mui.os.plus) {
				mui.plusReady(function() {
					mui.back = function() {
						if (plus.webview.getWebviewById("articleView")) {
							plus.webview.close("articleView");
							return;
						}
						plus.runtime.quit();
					}
					setTimeout(function() {
						mui('#pullrefresh').pullRefresh().pullupLoading();
					}, 1000);
				});
			} else {
				mui.ready(function() {
					mui('#pullrefresh').pullRefresh().pullupLoading();
				});
			}
		</script>
	</body>

</html>